<template>
  <div class="model-container">
    <div style="background-color: white">
      <div class="listName">二级评论管理
        <el-button size="small" type="primary" plain @click="goBack">返回</el-button>
      </div>
    </div>

    <el-card shadow="never" />

    <el-card style="margin-top: 10px">
      <div class="table-wrapper">
        <el-table v-loading="!dataOption" :stripe="true" :data="dataOption" border empty-text="暂无数据" @selection-change="changeFun">

          <el-table-column align="center" width="55" label="序号" type="index" />

          <el-table-column align="center" prop="nick" label="用户昵称" />

          <el-table-column align="center" prop="memberId" label="用户ID" />

          <el-table-column align="center" prop="content" label="评论内容" />

          <el-table-column align="center" prop="createTime" label="评论时间" />

          <el-table-column align="center" label="操作" width="500px">
            <template slot-scope="scope">
              <el-button type="primary" size="mini" plain @click="edit(scope.row)">修改</el-button>
              <el-button type="danger" size="mini" plain @click="del(scope.row)">删除</el-button>
              <el-button v-if="scope.row.isHidden === 0" type="warning" size="mini" plain @click="hide(scope.row)">隐藏</el-button>
              <el-button v-else type="warning" size="mini" plain @click="hide(scope.row)">显示</el-button>
            </template>
          </el-table-column>

        </el-table>

        <!--分页-->
        <el-pagination
          :current-page="pageNo"
          :page-sizes="[10, 20, 30]"
          :page-size="pageSize"
          :total="rowCount"
          background
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange" />
      </div>
    </el-card>

    <!--弹框-->
    <el-dialog :visible.sync="dialogState" :before-close="handleClose" title="修改评论" top="5vh" width="40%">
      <el-form ref="dialogBindData" :model="dialogBindData" :rules="rules" label-width="30%" status-icon class="demo-ruleForm">
        <div class="formBody">
          <el-form-item label="评论内容" prop="content" class="three">
            <el-input v-model="dialogBindData.content" :rows="5" size="small" type="textarea" placeholder="" resize="none" />
          </el-form-item>
          <div class="btns-wrapper">
            <el-button size="small" @click="handleClose">取 消</el-button>
            <el-button :loading="buttonState" type="primary" size="small" @click="submmitFun">保 存</el-button>
          </div>
        </div>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import * as video from '../../api/video.js'
export default {
  components: {

  },
  data() {
    return {
      // 列表数据
      dataOption: null,
      pageNo: 1,
      pageSize: 10,
      rowCount: null, // 总条数
      // 类型
      type: '',
      // 弹出框状态
      dialogState: false,
      dialogType: 1, // 1--增加  2--编辑  3--查看
      // 对话框表单数据对象
      dialogBindData: {
        id: '',
        content: ''
      },
      // 隐藏显示 0显示 1隐藏
      isHost: 0,
      rules: {
        content: [{
          required: true,
          message: '请输入评论内容',
          trigger: 'blur'
        }]
      },
      // 按钮状态
      buttonState: false,
      delBtn: true,
      delIdArr: [],
      videoData: null
    }
  },

  beforeMount() {
    this.editorData = this.$route.params.commentId // 编辑数据
    this.videoData = this.$route.params.data
    this.getList()
  },

  methods: {
    goBack() {
      this.$router.go(-1)
    },
    // 反显
    reBindData(data) {
      for (const key in this.dialogBindData) {
        this.dialogBindData[key] = data[key]
      }
    },
    // 删除
    changeFun(data) {
      this.datavalID = data
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.pageNo = val
      this.getList()
    },
    handleClose() {
      for (const key in this.dialogBindData) {
        if (key === 'sellerType') {
          this.dialogBindData[key] = 1
        } else if (key === 'settlementPeriod') {
          this.dialogBindData[key] = 1
        } else {
          this.dialogBindData[key] = ''
        }
      }
      this.dialogState = false
      this.disabled = false
      this.disableIinput = false
      // this.$refs.dialogBindData.clearValidate()
    },
    // 查询列表数据
    getList() {
      const param = {
        pid: this.editorData,
        current: this.pageNo,
        size: this.pageSize
      }
      video.getSecondCommentById(param).then(res => {
        console.log('查询二级评论列表', res)
        if (res.data.code !== 1) {
          this.$message.error(res.data.msg)
          return false
        }
        if (res.data.data === 'null' || res.data.data === null) {
          return false
        }
        // 初始话表格数据
        this.dataOption = res.data.data.records
        // 初始化分页数据
        this.rowCount = res.data.data.total
        this.pageSize = res.data.data.size
      })
    },
    add() {
      this.dialogType = 1
      this.dialogState = true
    },
    // 编辑
    edit(data) {
      this.dialogType = 2
      this.dialogState = true
      this.disabled = true
      this.dialogBindData.id = data.id
      this.reBindData(data)
    },
    // 隐藏
    hide(data) {
      if (data === 0) {
        if (this.datavalID.length === 0) {
          this.$message({
            type: 'warning',
            message: '请选择商家!'
          })
        }
        return false
      }
      let str
      if (data.isHidden === 0) {
        str = '确认要隐藏评论吗,是否继续?'
      } else {
        str = '确认要显示评论吗,是否继续?'
      }

      this.$confirm(str, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.hideDataTable(data)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    },
    // 删除
    del(data) {
      if (data === 0) {
        if (this.datavalID.length === 0) {
          this.$message({
            type: 'warning',
            message: '请选择商家!'
          })
        }
        return false
      }
      this.$confirm('确认要删除此评论吗,是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.delDataTable(data)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    },
    // 提交
    submmitFun() {
      const param = {
        id: this.dialogBindData.id,
        content: this.dialogBindData.content
      }

      this.$refs.dialogBindData.validate((valid) => {
        if (valid) {
          video.updateVideoComment(param).then(res => {
            if (res.data.code !== 1) {
              this.$message({
                message: res.data.msg,
                type: 'warning'
              })
              this.buttonState = false
              return false
            }
            this.$message({
              message: '编辑成功',
              type: 'success'
            })
            this.buttonState = false
            this.getList()
            this.handleClose()
          })
        } else {
          return false
        }
      })
    },
    // 获取删除分类集合
    delDataTable(data) {
      const ids = []
      ids.push(data.id)
      video.deleteVideoComment(ids).then(res => {
        if (res.data.code === 1) {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getList()
        }
      })
    },
    // 隐藏显示评论
    hideDataTable(data) {
      console.log('需要隐藏的评论内容', data)
      const param = {
        id: data.id,
        isHidden: data.isHidden === 0 ? 1 : 0
      }

      video.hideVideoComment(param).then(res => {
        if (res.data.code !== 1) {
          this.$message({
            message: res.data.msg,
            type: 'warning'
          })
          this.buttonState = false
          return false
        }
        this.$message({
          message: '操作成功',
          type: 'success'
        })
        this.buttonState = false
        this.getList()
        this.handleClose()
      })
    }

  }
}
</script>

<style>
	.listName {
		margin: 0;
		padding: 15px;
		display: flex;
		align-items: center;
		justify-content: space-between;
	}
</style>
